package org.lex.ftplist.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;

import org.lex.collection.tree.ITreeNode;
import org.lex.collection.tree.diff.TreeComparator;
import org.lex.ftplist.core.CacheService;
import org.lex.ftplist.core.FtpUrl;
import org.lex.ftplist.core.Index;
import org.lex.ftplist.core.diff.FTPNodeComparator;
import org.lex.ftplist.ui.action.FLAction;
import org.lex.ftplist.ui.util.FtpTreeUtils;
import org.netbeans.swing.outline.Outline;

public class FtpTreeFrame extends JInternalFrame {

	private Outline tree = null;
	private JPanel pane = null;
	private JToolBar toolbar = null;
	private FtpUrl url = null;
	private ITreeNode root = null;

	public FtpTreeFrame(FtpUrl url, ITreeNode root) {
		super("FTP Tree", true, true, true, true);
		this.url = url;
		this.root = root;
		this.setSize(300, 400);
		this.initComponents();
	}

	private void initComponents() {
		this.setContentPane(this.getPane());
	}

	private JPanel getPane() {
		if (null == pane) {
			pane = new JPanel(new BorderLayout());
			pane.add(this.getToolBar(), BorderLayout.NORTH);

			tree = FtpTreeUtils.createFtpListTree(root);
			pane.add(new JScrollPane(tree), BorderLayout.CENTER);
		}
		return pane;
	}

	private JToolBar getToolBar() {
		if (null == toolbar) {
			toolbar = new JToolBar();
			toolbar.add(new SaveFtpListAction());
			toolbar.add(new CompareFtpAction());
		}
		return toolbar;
	}

	class SaveFtpListAction extends FLAction {

		public SaveFtpListAction() {
			this.setName("Save");
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				CacheService.getInstance().store(url, root);
				JOptionPane.showMessageDialog(FtpTreeFrame.this, "Done");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	class CompareFtpAction extends FLAction {

		public CompareFtpAction() {
			this.setName("Compare");
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				Index index = ChooseCachedFtpDialog.showDialog();
				if (null == index) {
					return;
				}
				ITreeNode other = CacheService.getInstance().load(index);

				FTPNodeComparator cmp = new FTPNodeComparator();
				TreeComparator tc = new TreeComparator(cmp);
				tc.compareTree(other, root);
				ITreeNode diff = tc.buildDiffTree();
				System.out.println("DIFF:\n" + diff.toTreeString());
				JInternalFrame frame = this.createDiffFrame(diff);
				FLApplication.getInstance().getOperator().addInternalFrame(frame);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

		private JInternalFrame createDiffFrame(ITreeNode diffRoot) {
			JInternalFrame frame = new JInternalFrame("diff", true, true, true, true);
			Outline tree = FtpTreeUtils.createDiffTree(diffRoot);
			frame.setContentPane(new JScrollPane(tree));
			frame.setSize(400, 300);
			return frame;
		}
	}
}
